﻿function StoreMessagerController($scope, service, $routeParams, $location, $http) {

    $scope.myStore = "";
    $scope.storeMessage = "";
    var popupOptions = {
        appendTo: $("#popup-wrapper"),
        closeClass: 'b-close',
        speed: 300
    };
    //get data of Store list api/storelist/1
    $http.get(service.locationUrl + "/storelist/" + service.infoUser.Id, {})
        .success(function (data) {
            $scope.rowsStoreList = data;
        })
        .error(function (err) {

            console.log("error", err);
        });
    //end
    $scope.update = function () {
        if ($scope.myStore != "") {
            $http.get(service.locationUrl + "/companystore/companyId/" + service.infoUser.Id + "/storeid/" + $scope.myStore, {})
            .success(function (data) {
                $scope.storeMessage = data;
            })
            .error(function (err) {

            });
        } else {
            $scope.storeMessage = "";
        }
    }

    $scope.updateMessage = function () {
        $('.popup-delete-save').bPopup(popupOptions);
         $scope.title = "Do you save new welcome message ?";
        var storeObj = {
            CompanyId: service.infoUser.Id,
            WelcomeMessage: $scope.storeMessage.WelcomeMessage
        }
        $scope.action = function () {
            if ($scope.myStore == "") {
                for (var i = 0; i < $scope.rowsStoreList.length; i++) {
                    $http.put(
                       service.locationUrl + "/companystore/" + $scope.rowsStoreList[i].StoreId,
                       storeObj)
                   .success(function () {
                   })
                   .error(function () { });
                }
                $('.popup-delete-save').bPopup().close();
            }
            else {
                $http.put(
                        service.locationUrl + "/companystore/" + $scope.myStore,
                        storeObj)
                    .success(function () {
                        $('.popup-delete-save').bPopup().close();
                    })
                    .error(function () { });
            }
        }       
    }
}

